package leetcode.数组;

public class 多数元素169 {
    public static int majorityElement(int[] nums) {
        java.util.HashMap<Integer,Integer> map = new java.util.HashMap<>();
        int maxKey = nums[0];
        map.put(nums[0],1);
        for (int i = 1; i < nums.length; i++) {
            int num = nums[i];
            if(map.containsKey(num)){
                int newNum = map.get(num) +1;
                if(newNum > map.get(maxKey))
                    maxKey = num;
                map.put(num,newNum );
            }else {
                map.put(num,1);
            }
        }
        return maxKey;
    }

    public static void main(String[] args) {
        int[] nums = {3,2,3};
        System.out.println(majorityElement(nums));
    }
}
